home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / ubiquity / bin / ubiquity
Encoding:
Text File  |  2008-10-29  |  7.5 KB  |  232 lines

  1. #!/usr/bin/python
  2.  
  3. '''
  4. Installer
  5.  
  6. This is a installer program for a Ubuntu or Metadistros Live system.
  7. This is the main program, but there are also a couple of libraries to
  8. help it to work, such as the frontend.
  9. The way it works is simple. It detects the frontend to use, then
  10. load the module for that frontend. After that, it makes some calls
  11. through the frontend in order to get the info necessary to install.
  12.  
  13. Once it has the info, partitioning, format, copy the distro to the disk
  14. and configure everything.
  15. '''
  16.  
  17. import sys
  18. import os
  19. import errno
  20. import fcntl
  21. import shutil
  22. import syslog
  23. import atexit
  24. import optparse
  25. import subprocess
  26.  
  27. sys.path.insert(0, '/usr/lib/ubiquity')
  28.  
  29. from ubiquity import misc
  30.  
  31. VERSION = '1.10.10'
  32. TARGET = '/target'
  33. LOCKFILE = '/var/lock/ubiquity'
  34. lock = None
  35.  
  36. def distribution():
  37.     """Returns the name of the running distribution."""
  38.  
  39.     proc = subprocess.Popen(['lsb_release', '-is'], stdout=subprocess.PIPE)
  40.     return proc.communicate()[0].strip()
  41.  
  42. def install(frontend=None):
  43.     '''install(frontend=None) -> none
  44.     
  45.     Get the type of frontend to use and load the module for that.
  46.     If frontend is None, defaults to the first of mythbuntu_ui, 
  47.     gtk_ui, and kde_ui that exists.
  48.     '''
  49.     if frontend is None:
  50.         frontends = ['mythbuntu_ui', 'gtk_ui', 'kde_ui']
  51.     else:
  52.         frontends = [frontend]
  53.     mod = __import__('ubiquity.frontend', globals(), locals(), frontends)
  54.     for f in frontends:
  55.         if hasattr(mod, f):
  56.             ui = getattr(mod, f)
  57.             # Noninteractive implies automatic mode.
  58.             if f == 'noninteractive':
  59.                 os.environ['UBIQUITY_AUTOMATIC'] = '1'
  60.             break
  61.     else:
  62.         raise AttributeError, ('No frontend available; tried %s' %
  63.                                ', '.join(frontends))
  64.  
  65.     unmount_target()
  66.     distro = distribution().lower()
  67.     wizard = ui.Wizard(distro)
  68.     ret = wizard.run()
  69.     copy_debconf()
  70.     unmount_target()
  71.     if ret == 10:
  72.         wizard.do_reboot()
  73.  
  74. def copy_debconf():
  75.     """Copy a few important questions into the installed system."""
  76.     targetdb = '/target/var/cache/debconf/config.dat'
  77.     # xserver-xorg is temporary, pending a rework of bullet-proof-x; but
  78.     # note that xserver-xorg/config/inputdevice/keyboard/* is still needed
  79.     for q in ('^console-setup/','^xserver-xorg/'):
  80.         misc.execute('debconf-copydb', 'configdb', 'targetdb', '-p', q,
  81.                      '--config=Name:targetdb', '--config=Driver:File',
  82.                      '--config=Filename:%s' % targetdb)
  83.  
  84. def unmount_target():
  85.     paths = []
  86.     mounts = open('/proc/mounts')
  87.     for line in mounts:
  88.         path = line.split(' ')[1]
  89.         if path == '/target' or path.startswith('/target/'):
  90.             paths.append(path)
  91.     mounts.close()
  92.     paths.sort()
  93.     paths.reverse()
  94.     for path in paths:
  95.         misc.execute('umount', path)
  96.  
  97. def prepend_path(directory):
  98.     if 'PATH' in os.environ and os.environ['PATH'] != '':
  99.         os.environ['PATH'] = '%s:%s' % (directory, os.environ['PATH'])
  100.     else:
  101.         os.environ['PATH'] = directory
  102.  
  103. def release_lock():
  104.     global lock
  105.     try:
  106.         os.unlink(LOCKFILE)
  107.     except OSError:
  108.         pass
  109.     if lock is not None:
  110.         lock.close()
  111.         lock = None
  112.  
  113. def acquire_lock():
  114.     global lock
  115.     lock = open(LOCKFILE, 'w')
  116.     try:
  117.         fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
  118.     except IOError, e:
  119.         if e.errno in (errno.EACCES, errno.EAGAIN, errno.EWOULDBLOCK):
  120.             print "Ubiquity is already running!"
  121.             sys.exit(1)
  122.         raise
  123.     atexit.register(release_lock)
  124.     fcntl.fcntl(lock, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
  125.     print >>lock, os.getpid()
  126.     lock.flush()
  127.     os.fsync(lock.fileno())
  128.  
  129. def main():
  130.     usage = '%prog [options] [frontend]'
  131.     parser = optparse.OptionParser(usage=usage, version=VERSION)
  132.     parser.set_defaults(
  133.         debug=('UBIQUITY_DEBUG' in os.environ),
  134.         debug_pdb=False,
  135.         cdebconf=False,
  136.         automatic=False,
  137.         migration_assistant=True)
  138.     parser.add_option('-d', '--debug', dest='debug', action='store_true',
  139.                       help='debug mode (warning: passwords will be logged!)')
  140.     parser.add_option('--pdb', dest='debug_pdb', action='store_true',
  141.                       help='drop into Python debugger on a crash')
  142.     parser.add_option('--cdebconf', dest='cdebconf', action='store_true',
  143.                       help='use cdebconf instead of debconf (experimental)')
  144.     parser.add_option('--no-migration-assistant', dest='migration_assistant',
  145.                       action='store_false',
  146.                       help='disable Migration Assistant')
  147.     parser.add_option('--automatic', dest='automatic', action='store_true',
  148.                       help='do not ignore the "seen" flag (useful for ' \
  149.                       'unattended installations).')
  150.     parser.add_option('--old-tzmap', dest='old_tzmap', action='store_true',
  151.                       help='use the old timezone map.')
  152.     parser.add_option('--only', dest='only', action='store_true',
  153.                       help='tell the application that it is the only desktop ' \
  154.                       'program running so that it can customize its UI to ' \
  155.                       'better suit a minimal environment.')
  156.     (options, args) = parser.parse_args()
  157.  
  158.     if options.debug:
  159.         os.environ['UBIQUITY_DEBUG'] = '1'
  160.  
  161.     if options.debug_pdb:
  162.         os.environ['UBIQUITY_DEBUG_PDB'] = '1'
  163.  
  164.     if options.cdebconf:
  165.         # Note that this needs to be set before DebconfCommunicate is
  166.         # imported by anything.
  167.         os.environ['DEBCONF_USE_CDEBCONF'] = '1'
  168.         prepend_path('/usr/lib/cdebconf')
  169.     prepend_path('/usr/lib/ubiquity/compat')
  170.  
  171.     if options.automatic:
  172.         os.environ['UBIQUITY_AUTOMATIC'] = '1'
  173.  
  174.     if options.migration_assistant:
  175.         os.environ['UBIQUITY_MIGRATION_ASSISTANT'] = '1'
  176.  
  177.     if options.old_tzmap:
  178.         os.environ['UBIQUITY_OLD_TZMAP'] = '1'
  179.  
  180.     if options.only:
  181.         os.environ['UBIQUITY_ONLY'] = '1'
  182.  
  183.     acquire_lock()
  184.  
  185.     if not os.path.exists('/var/log/installer'):
  186.         os.makedirs('/var/log/installer')
  187.     syslog.openlog('ubiquity', syslog.LOG_NOWAIT | syslog.LOG_PID)
  188.  
  189.     syslog.syslog("Ubiquity %s" % VERSION)
  190.     version_file = open('/var/log/installer/version', 'w')
  191.     print >>version_file, 'ubiquity %s' % VERSION
  192.     version_file.close()
  193.  
  194.     if 'UBIQUITY_DEBUG' in os.environ:
  195.         if 'UBIQUITY_DEBUG_CORE' not in os.environ:
  196.             os.environ['UBIQUITY_DEBUG_CORE'] = '1'
  197.         if 'DEBCONF_DEBUG' not in os.environ:
  198.             os.environ['DEBCONF_DEBUG'] = 'developer|filter'
  199.     # The frontend should take care of displaying a helpful message if
  200.     # we are being run without root privileges.
  201.     if not (args and args[0] == 'noninteractive'):
  202.         try:
  203.             sys.stderr = open('/var/log/installer/debug', 'a', 1)
  204.             os.dup2(sys.stderr.fileno(), 2)
  205.             print >>sys.stderr, "Ubiquity %s" % VERSION
  206.         except IOError, err:
  207.             if err.errno != errno.EACCES:
  208.                 raise
  209.  
  210.     # Default to enabling internal (non-debconf) debugging except for when
  211.     # using --automatic.
  212.     if 'UBIQUITY_DEBUG_CORE' not in os.environ:
  213.         if options.automatic:
  214.             os.environ['UBIQUITY_DEBUG_CORE'] = '1'
  215.  
  216.     # Clean up old state.
  217.     for name in ('apt-installed', 'apt-install-direct', 'remove-kernels'):
  218.         path = os.path.join('/var/lib/ubiquity', name)
  219.         if os.path.exists(path):
  220.             os.unlink(path)
  221.     shutil.rmtree("/var/lib/partman", ignore_errors=True)
  222.  
  223.     if args:
  224.         install(args[0])
  225.     else:
  226.         install()
  227.  
  228. if __name__ == '__main__':
  229.     main()
  230.  
  231. # vim:ai:et:sts=4:tw=80:sw=4:
  232.